package ccuserinterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;

import datamodels.Conference;
import datamodels.GlobalDataStore;
import datamodels.Manuscript;
import datamodels.Review;
import datamodels.Role;
import datamodels.User;
/**
 * A frame for reviewers submit a review.
 * @author Zach Turk
 *
 */
@SuppressWarnings("serial")
public class SubmitAReviewScreen extends JFrame
{
	JTable my_jtable;
	JScrollPane my_scroll_pane;
	GlobalDataStore my_data_store;
	JButton my_back_btn;
	Role my_role;
	Conference my_conference;
	User my_user;
	List<Manuscript> manuscripts;
	Manuscript my_man;
	/**constructor */
	public SubmitAReviewScreen(GlobalDataStore the_data_store, Role the_role, Conference the_conference, User the_user)
	{
		super();
		my_data_store = the_data_store;
		my_role = the_role;
		my_conference = the_conference;
		my_user = the_user;
    	setTitle("View Manuscripts");
        setLayout(null);
        setVisible(true);
        setPreferredSize(new Dimension(500,400));
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
		setupGUI();
		pack();
	}
	/**
	 * set up the frame for reviewer submit a review.
	 */
	private void setupGUI()
	{
		
		if (my_user.getManuscriptsToReviewList() == null || my_user.getManuscriptsToReviewList().size() == 0) {
			//TODO User has nothing to review, let them know this.
                int option = JOptionPane.showConfirmDialog(null,"You have no assigned reviews.  Log out?", 
                		"Confirm", JOptionPane.YES_NO_OPTION);
                if (option == 0) {
                	dispose(); 
                	new ConferenceCreatorStart();
                }
		}
		manuscripts = my_user.getManuscriptsToReviewList();
		String[] dropdown_options = new String[manuscripts.size()];
			System.out.println();
		for (int i = 0; i < manuscripts.size(); i++) {
			System.out.print(manuscripts.get(i).getTitle());
			dropdown_options[i] = manuscripts.get(i).getTitle();
		}
			System.out.println();
		
		//drop down list to choose a paper
		final JComboBox manuscript_selection = new JComboBox(dropdown_options);
		manuscript_selection.setSelectedIndex(0);
		my_man = manuscripts.get(manuscript_selection.getSelectedIndex());
		manuscript_selection.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				my_man = manuscripts.get(manuscript_selection.getSelectedIndex());
				
			}
		});
		manuscript_selection.setBounds(5, 5, 200, 20);
		add(manuscript_selection);
		

		//the log out button that will take u back to the main screen.
		JButton logOut = new JButton("Log Out");
		logOut.setBounds(410, 5 , 80 , 20);
		logOut.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub   
                int option = JOptionPane.showConfirmDialog(null,"Are you sure you want to log out?", 
                		"Confirm", JOptionPane.YES_NO_OPTION);
                if (option == 0) {
                	dispose(); 
                	new ConferenceCreatorStart();
                }
			}
		});
		add(logOut);
		
		String author = "Author: " + my_man.getAuthor().getName();
		JLabel l_name = new JLabel(author);
		l_name.setBounds(25, 35, 200, 20);
		add(l_name);
		String title = "Title: " + my_man.getTitle();
		JLabel l_title = new JLabel(title);
		l_title.setBounds(25, 55, 200, 20);
		add(l_title);
		JTextArea text = new JTextArea();
		text.setText(my_man.getText());
		text.setLineWrap(true);
		text.setWrapStyleWord(true);
		text.setEditable(false);
		text.setBounds(5, 35, 200, 275);
		JPanel left_panel  = new JPanel(); 
		left_panel.setBackground(Color.WHITE);
		left_panel.add(text);
		JScrollPane left_pane = new JScrollPane(left_panel);
		left_pane.setBounds(15, 80, 225, 260);
		add(left_pane);
		
		//the right panel
		String reviewers = "Review: ";
		String reviews ="Type your review here:";
		JLabel r_name = new JLabel(reviewers);
		r_name.setBounds(265, 35, 200, 20);
		add(r_name);
		final JTextArea rev_field = new JTextArea();
		rev_field.setText(reviews);
		rev_field.setLineWrap(true);
		rev_field.setWrapStyleWord(true);
		rev_field.setEditable(true);
		rev_field.setBounds(5, 35, 200, 275);
		JPanel right_panel  = new JPanel(); 
		right_panel.setBackground(Color.WHITE);
		right_panel.add(rev_field);
		JScrollPane right_pane = new JScrollPane(right_panel);
		right_pane.setBounds(255, 80, 225, 260);
		add(right_pane);
		
		//for the bottom buttons.
		JButton go = new JButton("Submit Review");
		go.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				my_man.addReview(new Review(my_user.getName(), rev_field.getText()));
				try {
					my_data_store.writeData();
                int option = JOptionPane.showConfirmDialog(null,"Review submitted.  Log out?", 
                		"Confirm", JOptionPane.YES_NO_OPTION);
                if (option == 0) {
                	dispose(); 
                	new ConferenceCreatorStart();
                }
				} catch (IOException e) {}
			}		
		});
		go.setBounds(375, 350, 100, 20);
		add(go);
	}
}